package com.car.car.controller;


import com.car.car.VO.ResultVO;
import com.car.car.converter.OrderForm2OrderDTOConverter;
import com.car.car.dto.OrderDTO;
import com.car.car.enums.ResultEnums;
import com.car.car.exception.SellException;
import com.car.car.form.OrderForm;
import com.car.car.service.BuyerService;
import com.car.car.service.OrderService;
import com.car.car.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult)
    {
        if(bindingResult.hasErrors()){
            log.error("create_params{}",orderForm);
            throw new SellException(ResultEnums.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);

        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            throw new SellException(ResultEnums.CART_EMPTY);
        }

        OrderDTO createResult = orderService.create(orderDTO);

        Map<String,String> map = new HashMap<>();
        map.put("orderId",createResult.getOrderId());

        return ResultUtil.success(map);
    }


    @PostMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size)
    {
        if(StringUtils.isEmpty(openid)){
            throw new SellException(ResultEnums.PARAM_ERROR);
        }

        PageRequest request = new PageRequest(page,size);

        Page<OrderDTO> orderDTOPage =  orderService.findList(openid,request);

        return ResultUtil.success(orderDTOPage.getContent());
    }


    @PostMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        OrderDTO orderDTO = buyerService.findOrderOne(openid,orderId);
        return ResultUtil.success(orderDTO);
    }


    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){


        OrderDTO orderDTO = buyerService.cancelOrder(openid,orderId);
        return ResultUtil.success();
    }

}
